#coding:utf-8
from django.shortcuts import render
from django.shortcuts import redirect
from app01 import models
import hashlib

# Create your views here.

def login(request):
    if request.method=='POST':
        input_uname=request.POST['uname']
        input_pwd=request.POST['pwd']
        input_pwd=hashlib.md5(input_pwd).hexdigest()
        user_list=models.UserInfo.objects.all()
        for user in user_list:
            if input_uname==user.uname and input_pwd==user.pwd:
                return redirect('/usermanage/')
        else:
            return render(request,'login.html',{"status":"用户名密码错误！"})
    return render(request, 'login.html')

def usermanage(request):
    if request.method=='POST':
        #当请求方法为POST时，判断提交的那么属性名称在不在这个请求列表里，如果在，就执行对应的代码；
        #1、重置密码的name被提交
        #2、创建单个用户被提交
        #3、创建多个用户被提交
        if 'uname_reset' in request.POST:
            input_uname=request.POST['uname_reset']
            #重置密码：获取用户在input输入的用户名，根据name属性定位，在数据库中找到这个用户，密码重置为123；
            models.UserInfo.objects.filter(uname=input_uname).update(pwd='202cb962ac59075b964b07152d234b70')
            #判断现在表中密码是否为123
            for user in models.UserInfo.objects.all():
                if user.pwd=='202cb962ac59075b964b07152d234b70':
                    return render(request,'usermanage.html',{"ret":'重置成功！'})
                else:
                    return render(request,'usermanage.html',{"ret":'重置失败！'})
        elif 'uname_add' in request.POST:
            #===创建用户===,和注册时使用的一样的代码，所以公用一个函数add_user(request,name)
            # add_user(request,'uname_add','usermanage.html')
            input_uname=request.POST['uname_add']
            input_email=request.POST['email']
            input_pwd=request.POST['pwd']
            input_pwd=hashlib.md5(input_pwd).hexdigest()
            #判断有无此用户
            user_list=models.UserInfo.objects.all()
            for user in user_list:
                if user.uname==input_uname:
                    return render(request,'usermanage.html',{'status':'此用户已被注册'})
            #新建用户数据
            models.UserInfo.objects.create(uname=input_uname,pwd=input_pwd,email=input_email)
            #判断是否成功
            user_list=models.UserInfo.objects.all()
            for user in user_list:
                if user.uname==input_uname:
                    #注册成功，立即登录
                    return render(request,'usermanage.html',{'status':'注册成功','a':'立即登录'})
            else:
                return render(request,'usermanage.html',{'status':'注册失败'})
        elif "multi_add" in request.POST:
            input_u=request.POST['multi_add']         #u"user1,user1@qq.com,pwd1\r\nuser2,user2@qq.com,pwd2"
            input_str=input_u.encode('utf-8')         #"user1,user1@qq.com,pwd1\r\nuser2,user2@qq.com,pwd2"
            input_list=input_str.split('\r\n')        #["user1,user1@qq.com,pwd1","user2,user2@qq.com,pwd2"]
            for per_user in input_list:          #"user1,user1@qq.com,pwd1"或"user2,user2@qq.com,pwd2"
                input_uname = per_user.split(",")[0]   #user1
                input_email = per_user.split(",")[1]    #user1@qq.com
                input_pwd = per_user.split(",")[2]    #pwd1
                input_pwd=hashlib.md5(input_pwd).hexdigest()
                #判断有无此用户
                user_list=models.UserInfo.objects.all()
                for user in user_list:
                    if user.uname==input_uname:
                        return render(request,'usermanage.html',{'status':input_uname+'用户已被注册'})
                #新建用户数据
                models.UserInfo.objects.create(uname=input_uname,pwd=input_pwd,email=input_email)
                #判断是否成功
                # user_list=models.UserInfo.objects.all()
                # for user in user_list:
                #     if user.uname==input_uname:
                #         #注册成功，立即登录
                #         return render(request,'usermanage.html',{'status':input_uname+'注册成功','a':'立即登录'})
                # else:
                #     return render(request,'usermanage.html',{'status':input_uname+'注册失败'})
    #获取数据库表中用户数据，生成列表，传给页面展示
    user_info_list = models.UserInfo.objects.all()
    return render(request, 'usermanage.html',{"user_info_list":user_info_list,})

def register(request):
    if request.method=='POST':
       # add_user(request,'uname','register.html')
        input_uname=request.POST['uname']
        input_email=request.POST['email']
        input_pwd=request.POST['pwd']
        input_pwd=hashlib.md5(input_pwd).hexdigest()
        #判断有无此用户
        user_list=models.UserInfo.objects.all()
        for user in user_list:
            if user.uname==input_uname:
                return render(request, 'register.html',{'status':'此用户已被注册'})
        #新建用户数据
        models.UserInfo.objects.create(uname=input_uname,pwd=input_pwd,email=input_email)
        #判断是否成功
        user_list=models.UserInfo.objects.all()
        for user in user_list:
            if user.uname==input_uname:
                #注册成功，立即登录
                return render(request, 'register.html',{'status':'注册成功','a':'立即登录'})
        else:
            return render(request, 'register.html',{'status':'注册失败'})
    return render(request, 'register.html')

def add_user(request,name,html):
    #调用这个函数时，有点问题，status文字不能显示。
     #===创建用户===
    input_uname=request.POST[name]
    input_pwd=request.POST['pwd']
    input_pwd=hashlib.md5(input_pwd).hexdigest()
    #判断有无此用户
    user_list=models.UserInfo.objects.all()
    for user in user_list:
        if user.uname==input_uname:
            return render(request,html,{'status':'此用户已被注册'})
    #新建用户数据
    models.UserInfo.objects.create(uname=input_uname,pwd=input_pwd)
    #判断是否成功
    user_list=models.UserInfo.objects.all()
    for user in user_list:
        if user.uname==input_uname:
            #注册成功，立即登录
            return render(request,html,{'status':'注册成功','a':'立即登录'})
    else:
        return render(request,html,{'status':'注册失败'})